home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / Subversive Component Hacks / LousyConnection / LousyConnection.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  4.1 KB  |  158 lines

  1. #include <Movies.h>
  2. #include <QuickTimeStreaming.h>
  3. #include <QTStreamingComponents.h>
  4. #include <FixMath.h>
  5.  
  6. #include "LousyConnection.h"
  7.  
  8. // data structures
  9.  
  10. typedef struct {
  11.     Component victim;
  12.     Fixed     dropRate;    // 0 -> never drop, fixed1 -> always drop
  13. } LousyConnectionSharedGlobalsRecord, **LousyConnectionSharedGlobalsHandle;
  14.  
  15. typedef struct{
  16.     ComponentInstance                    self;
  17.     ComponentInstance                    delegateComponent;
  18.     ComponentInstance                    target;
  19.     LousyConnectionSharedGlobalsHandle    sharedGlobals;
  20.     UInt32                                eventCount;
  21. } LousyConnectionGlobalsRecord, *LousyConnectionGlobalsPtr;
  22.  
  23. // dispatcher
  24.  
  25. #define RTPRSSM_BASENAME()                 LousyConnection
  26. #define RTPRSSM_GLOBALS()                 LousyConnectionGlobalsPtr storage
  27.  
  28. #define CALLCOMPONENT_BASENAME()        RTPRSSM_BASENAME()
  29. #define    CALLCOMPONENT_GLOBALS()            RTPRSSM_GLOBALS()
  30.  
  31. #define COMPONENT_UPP_PREFIX()            uppRTPRssm
  32. #define COMPONENT_DISPATCH_FILE            "LousyConnectionDispatch.h"
  33. #define COMPONENT_SELECT_PREFIX()          kRTPRssm
  34.  
  35. #define    GET_DELEGATE_COMPONENT()        (storage->delegateComponent)
  36.  
  37. #include "QTStreamingComponents.k.h"
  38. #include "ComponentDispatchHelper.c"
  39.  
  40. // code
  41.  
  42. pascal ComponentResult LousyConnectionOpen(LousyConnectionGlobalsPtr glob, ComponentInstance self)
  43. {
  44.     ComponentResult err;
  45.     LousyConnectionSharedGlobalsHandle sharedGlobals;
  46.  
  47.     glob = (LousyConnectionGlobalsPtr)NewPtrClear(sizeof(LousyConnectionGlobalsRecord));
  48.     err = MemError();
  49.     if (noErr != err) goto bail;
  50.  
  51.     SetComponentInstanceStorage(self, (Handle)glob);
  52.     glob->self = self;
  53.     glob->target = self;
  54.  
  55.     sharedGlobals = (LousyConnectionSharedGlobalsHandle)GetComponentRefcon((Component)self);
  56.     if (!sharedGlobals) {
  57.         Component victim;
  58.         ComponentDescription cd;
  59.  
  60.         GetComponentInfo((Component)self, &cd, nil, nil, nil);
  61.         cd.componentManufacturer = kVictimManufacturer;
  62.         cd.componentFlags = 0;
  63.         cd.componentFlagsMask = 0;
  64.         victim = FindNextComponent(nil, &cd);
  65.         if (!victim) {
  66.             DebugStr("\p couldn't find victim!");
  67.             err = paramErr;
  68.             goto bail;
  69.         }
  70.  
  71.         sharedGlobals = (LousyConnectionSharedGlobalsHandle) NewHandleSysClear(sizeof(LousyConnectionSharedGlobalsRecord));
  72.         if (!sharedGlobals) {
  73.             err = MemError();
  74.             goto bail;
  75.         }
  76.         SetComponentRefcon((Component)self, (long)sharedGlobals);
  77.  
  78.         victim = CaptureComponent(victim, (Component)self);
  79.         if (!victim) {
  80.             err = paramErr;
  81.             goto bail;
  82.         }
  83.         (**sharedGlobals).victim = victim;
  84.         (**sharedGlobals).dropRate = fixed1 / 2;
  85.     }
  86.  
  87.     glob->sharedGlobals = sharedGlobals;
  88.     err = OpenAComponent((**sharedGlobals).victim, &glob->delegateComponent);
  89.     if (err) goto bail;
  90.  
  91.     ComponentSetTarget(glob->delegateComponent, self);
  92.  
  93. bail:
  94.     return err;
  95. }
  96.  
  97. pascal ComponentResult LousyConnectionRegister(LousyConnectionGlobalsPtr glob)
  98. {
  99.     // If we get this far, we must have been opened successfully, so all is well.
  100.     return noErr;
  101. }
  102.  
  103. pascal ComponentResult LousyConnectionUnregister(LousyConnectionGlobalsPtr glob)
  104. {
  105.     UncaptureComponent((**glob->sharedGlobals).victim);
  106.     DisposeHandle((Handle)glob->sharedGlobals);
  107.     return noErr;
  108. }
  109.  
  110. pascal ComponentResult LousyConnectionClose(LousyConnectionGlobalsPtr glob, ComponentInstance self)
  111. {
  112.     if (glob) {
  113.         if (glob->delegateComponent)
  114.             CloseComponent(glob->delegateComponent);
  115.  
  116.         DisposePtr((Ptr)glob);
  117.     }
  118.  
  119.     return noErr;
  120. }
  121.  
  122. pascal ComponentResult LousyConnectionTarget(LousyConnectionGlobalsPtr glob, ComponentInstance target)
  123. {
  124.     glob->target = target;
  125.     if (glob->delegateComponent)
  126.         ComponentSetTarget(glob->delegateComponent, target);
  127.  
  128.     return noErr;
  129. }
  130.  
  131. pascal ComponentResult LousyConnectionVersion(LousyConnectionGlobalsPtr glob)
  132. {
  133.     return CallComponentVersion(glob->delegateComponent);
  134. }
  135.  
  136. static Boolean capsLockIsDown(void)
  137. {
  138.     KeyMapByteArray    keys;
  139.     GetKeys((UInt32*)keys);
  140.     return (keys[7] & 2) ? true : false;
  141. }
  142.  
  143. pascal ComponentResult LousyConnectionHandleNewPacket(LousyConnectionGlobalsPtr glob, QTSStreamBuffer *inStreamBuffer, SInt32 inNumWraparounds)
  144. {
  145.     Boolean dropThisOne = false;
  146.     Fixed dropRate = (**glob->sharedGlobals).dropRate;
  147.     
  148.     dropThisOne = ((UInt16)Random()) < dropRate;
  149.     
  150.     if( dropThisOne ) {
  151.         QTSFreeMessage(inStreamBuffer);
  152.         return noErr;
  153.     }
  154.     else {
  155.         return RTPRssmHandleNewPacket(glob->delegateComponent, inStreamBuffer, inNumWraparounds);
  156.     }
  157. }
  158.